using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Universal.Api.Attributes;
using Universal.Application.Contracts.Dto.User;
using Universal.Application.Repositories;

namespace Universal.Api.Controllers;

/// <summary>
/// 用户控制器
/// </summary> <summary>
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
    private readonly IUserService _userService;
    public UsersController(IUserService userService)
    {
        _userService = userService;
    }

    //获取个人中心
    [HttpGet("profile")]
    public async Task<IActionResult> GetCurrentUserProfileAsync([FromQuery] Guid userId)
    {
        var result = await _userService.GetCurrentUserProfileAsync(userId);
        return Ok(result);
    }

    //修改个人中心
    [HttpPut("profile")]
    public async Task<IActionResult> UpdateUserProfileAsync([FromQuery] Guid userId, [FromBody] UpdateUserProfileDto updateUserProfileDto)
    {
        var result = await _userService.UpdateUserProfileAsync(userId, updateUserProfileDto);
        return Ok(result);
    }

    //分页获取用户列表
    [HttpGet("paged")]
    [RequirePermission("user.View")]
    public async Task<IActionResult> GetUsersPagedAsync([FromQuery] int pageIndex, [FromQuery] int pageSize)
    {
        var result = await _userService.GetUsersPagedAsync(pageIndex, pageSize);
        return Ok(result);
    }

    // 模糊查询用户列表
    [HttpGet("query")]
    [RequirePermission("user.View")]
    public async Task<IActionResult> QueryUsersAsync([FromQuery] UserQueryDto query)
    {
        var result = await _userService.QueryUsersAsync(query);
        return Ok(result);
    }

    // 启用用户
    [HttpPut("enable")]
    [RequirePermission("user.Edit")]
    public async Task<IActionResult> EnableUserAsync([FromQuery] Guid userId)
    {
        var result = await _userService.EnableUserAsync(userId);
        return Ok(result);
    }

    // 禁用用户
    [HttpPut("disable")]
    [RequirePermission("user.Edit")]
    public async Task<IActionResult> DisableUserAsync([FromQuery] Guid userId)
    {
        var result = await _userService.DisableUserAsync(userId);
        return Ok(result);
    }

    // 新增用户
    [HttpPost]
    [RequirePermission("user.Create")]
    public async Task<IActionResult> AddUserAsync([FromBody] AddEditUserDto addUserDto)
    {
        var result = await _userService.AddUserAsync(addUserDto);
        return Ok(result);
    }

    // 编辑用户
    [HttpPut("{id}")]
    [RequirePermission("user.Edit")]
    public async Task<IActionResult> EditUserAsync([FromRoute] Guid id, [FromBody] AddEditUserDto editUserDto)
    {
        var result = await _userService.EditUserAsync(id, editUserDto);
        return Ok(result);
    }

    // 删除用户
    [HttpDelete("{id}")]
    [RequirePermission("user.Delete")]
    public async Task<IActionResult> DeleteUserAsync([FromRoute] Guid id)
    {
        var result = await _userService.DeleteUserAsync(id);
        return Ok(result);
    }

    // 更新头像
    [HttpPut("avatar/{userId}")]
    public async Task<IActionResult> UpdateAvatarAsync([FromRoute] Guid userId, [FromBody] UpdateAvatarDto dto)
    {
        var result = await _userService.UpdateAvatarAsync(userId, dto.AvatarUrl);
        return Ok(result);
    }

    [HttpGet("roles")]
    public async Task<IActionResult> GetQueryRolesAsync()
    {
        var result = await _userService.QueryRoleView();
        return Ok(result);
    }

}